package com.crm.system.controller.customerManager;


import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.system.service.customerManager.CustomerImportService;
import com.crm.system.service.customerManager.QccImportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;

/**
 * 企查查导入功能
 *
 * @author yuanyunfeng
 * @date 2021/4/21 10:59
 */
@RestController
@RequestMapping("/customer")
@Slf4j
public class QccImportController {

    @Autowired
    private QccImportService qccImportService;

    /**
     * 根据客户名称调用企查查导入
     *
     * @param response
     * @param customerName
     */
    @GetMapping("/qccImport")
    public DhResponse qccImport(HttpServletResponse response, @RequestParam(value = "customerName", required = true) String customerName) {

        try {
//            String importResult = qccImportService.qccImport(customerName);
//            String importResult = qccImportService.shuidiImport(customerName);
            String importResult = qccImportService.lvdunImport(customerName);
            if(StringUtils.isNotBlank(importResult)){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), importResult);
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.error("QccImportController qccImport error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * 根据网址调用企查查网获取信息
     *
     * @param response
     * @param customerName
     */
    @GetMapping("/qccByWebsite")
    public DhResponse qccByWebsite(HttpServletResponse response, @RequestParam(value = "website", required = true) String website) {

        try {
//        String queryResult = qccImportService.shuidiByWebsite(website);
        String queryResult = qccImportService.lvdunByWebsite(website);
//            String queryResult = qccImportService.qccByWebsite(website);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), queryResult);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("QccImportController qccByWebsite error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }

    }


}
